﻿using System.Collections.Generic;
using UnityEngine;

public abstract class FSMStateMB : MonoBehaviour
{
    [SerializeField] private List<FSMActionMB> actions = new List<FSMActionMB>();
    [SerializeField] private List<FSMTransitionMB> transitions;

    public void Reset()
    {
        if (actions != null && actions.Count > 0)
        {
            for (var i = 0; i < actions.Count; i++)
            {
                actions[i].Reset();
            }
        }
    }

    public void UpdateState()
    {
        ExecuteActions();
    }

    private void ExecuteActions()
    {
        if (actions != null && actions.Count > 0)
        {
            for (var i = 0; i < actions.Count; i++)
            {
                actions[i].Act();
            }
        }
    }

    public string ExecuteTransitions()
    {
        if (transitions != null && transitions.Count > 0)
        {
            foreach (FSMTransitionMB fsmTransitionMb in transitions)
            {
                if (fsmTransitionMb != null)
                {
                    if (fsmTransitionMb.fsmDecideMb != null)
                    {
                        if (fsmTransitionMb.fsmDecideMb.Decide())
                        {
                            return fsmTransitionMb.trueState.ToString();
                        }
                        else
                        {
                            return fsmTransitionMb.falseState.ToString();
                        }
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }

            return null;
        }
        else
        {
            return null;
        }
    }

    public abstract string IdentityName();
}